package com.sheng.whyclass.work.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.android.volley.toolbox.StringRequest;
import com.sheng.util.Constant;
import com.sheng.util.CustomToast;
import com.sheng.util.HttpProcessManager;
import com.sheng.util.VolleyManager;
import com.sheng.whyclass.R;
import com.sheng.whyclass.work.adapter.UpdateContentAdapter;
import com.sheng.whyclass.work.event.FindAllWorkEvent;

import de.greenrobot.event.EventBus;

public class UpdateWorkFragment extends Fragment {
    private View mRootView = null;
    private ListView workList;
    private UpdateContentAdapter adapter;
    private String student_id;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }




    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_answer, container, false);
        initView();
        findStudentWork();
        return mRootView;
    }

    private void initView() {
      /*  StudentInfoData.DataBean data = LoginDataHelper.getInstance().getLoginUserInfo();
        student_id =data.getId();*/
        adapter = new UpdateContentAdapter(getActivity());
        workList = (ListView) mRootView.findViewById(R.id.work_list);
        workList.setAdapter(adapter);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

    }

    @Override
    public void onDetach() {
        super.onDetach();
    }


    public void onEventMainThread(FindAllWorkEvent event) {


        int code = event.code;
        if (event.list == null) {
            return;
        }

        if (code == Constant.CODE_FAIL) {
            CustomToast.showToast(getString(R.string.net_fail));

            return;
        } else {
            adapter.setData(event.list);
        }

    }

    private void findStudentWork() {
        StringRequest request = HttpProcessManager.getInstance().findAllWork(Constant.HOST_STUDENT_WORK_ALL);
        VolleyManager.getInstance(getActivity().getApplicationContext()).addToRequestQueue(request);
    }




}
